#!/bin/bash
#
# Xiange Linux build scripts

# Short one-line description of this package.
DESCRIPTION="proftpd ftp server"

# Homepage, not used by Portage directly but handy for developer reference
HOMEPAGE="http://www.proftpd.org/"

# Point to any required sources; these will be automatically downloaded by
# gpkg. 
# $N = package name, such as autoconf, x-org
# $V = package version, such as 2.6.10

#SRC_URI="http://foo.bar.com/$N-$V.tar.bz2"
SRC_URI="http://mirrors.dev.shopex.cn/lnmpp/sources/proftpd-1.3.3d.tar.bz2"


# Binary package URI.
BIN_URI=""


# Runtime Depend
RDEPEND=""

# Build time depend
DEPEND="${RDEPEND}"



#init 
xgb_init()
{
    echo "init $N-$V$R build script..."
    #add web server running user
    id ftpd > /dev/null 2>&1
    if [ $? -ne 0 ]; then
        useradd -s /sbin/nologin -M  ftpd
    fi
}

#unpack
xgb_unpack()
{
    #unpard file from $XGPATH_SOURCE to current directory.
    echo "Unpacking to `pwd`"
    tar xvf $XGPATH_SOURCE/`basename $SRC_URI`
}

#config
xgb_config()
{
    echo "config $N-$V$R..."
    #fist, cd build directory
    cd $N-$V$R
    
    #second, add package specified config params to XGB_CONFIG
    XGB_CONFIG+=" --prefix=/usr/local/proftpd --with-modules=mod_sql:mod_sql_mysql:mod_quotatab:mod_quotatab_sql --with-includes=/usr/local/mysql/include/mysql  --with-libraries=/usr/local/mysql/lib/mysql
   "    
    #Third, call configure with $XGB_CONFIG
    ./configure $XGB_CONFIG
}

#build
xgb_build()
{
    echo "make $N-$V$R..."
    make
}

#check
xgb_check()
{
    echo "checking $N-$V$R.."
    #make check
}

#install
xgb_install()
{
    echo "install to $XGPATH_DEST..."
    CC_SRC=$(pwd) 
    #install everything to $XGPATH_DEST
    make DESTDIR=$XGPATH_DEST install    
    
    local F_DEST=$XGPATH_DEST/usr/local/proftpd
    #locate self
    local N_ME_AT=$XGPATH_LIB/$T/$N

    mkdir -pv $F_DEST/etc    
    cat $N_ME_AT/conf/proftpd.conf > $F_DEST/etc/proftpd.conf
    PROFTPD_PASSWORD=$(</dev/urandom tr -dc A-Za-z0-9 | head -c8) 
    sed -i "/proftpd:proftpd:/d" $XGPATH/password.txt 
    echo "proftpd:proftpd:"$PROFTPD_PASSWORD >> $XGPATH/password.txt
    sed -i  s,PROFTPD_PASSWORD,$PROFTPD_PASSWORD,g $F_DEST/etc/proftpd.conf
    sed -i  s,PROFTPD_UID,$(id ftpd | sed 's,uid=\([0-9]*\)\((.*\),\1,g' ),g $F_DEST/etc/proftpd.conf
    sed -i  s,PROFTPD_GID,$(id ftpd | sed 's,\(.*\) gid=\([0-9]*\)\((.*\),\2,g'),g $F_DEST/etc/proftpd.conf
    
    
    #self start
    mkdir -pv $XGPATH_DEST/etc/rc.d/init.d/
    cp $CC_SRC/contrib/dist/rpm/proftpd.init.d   $XGPATH_DEST/etc/rc.d/init.d/proftpd
    sed  -i s,/usr/local/sbin,/usr/local/proftpd/sbin,g $XGPATH_DEST/etc/rc.d/init.d/proftpd 
}

#post install
xgb_postinst()
{
    echo "running after package installed..."
    local N_ME_AT=$XGPATH_LIB/$T/$N
    #/usr/local/mysql/bin/mysql  -h127.0.0.1 -uroot  < $N_ME_AT/conf/init.sql
    PROFTPD_PASSWORD=$(grep "proftpd:proftpd:" $XGPATH/password.txt | awk -F: '{print $3}')
    MYSQLD_PASSWORD=$(grep "mysql:root:" $XGPATH/password.txt | awk -F: '{print $3}')
    sed s,PROFTPD_PASSWORD,$PROFTPD_PASSWORD,g $N_ME_AT/conf/init.sql | /usr/local/mysql/bin/mysql  -h127.0.0.1 -uroot  -p$MYSQLD_PASSWORD
    chmod +x /etc/rc.d/init.d/proftpd 
    chkconfig proftpd --level 3 on
    if [ -d /data/httpd ]; then
        chown ftpd:ftpd /data/httpd        
    fi
    service proftpd start
}

#pre remove
xgb_prerm()
{
    echo "running before package delete..."
}

#post remove
xgb_postrm()
{
    echo "running after package delete..."
}
